﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WCF;
using System.Web.Security;

namespace geekmeet
{
    public partial class Default : System.Web.UI.Page
    {
        protected GeekMeetService wcf = new WCF.GeekMeetService();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            SetStartPage();
            GetRandomUsers();

            if (Request.QueryString["action"] == "logout")
            { LogOut(); }

            if (Request.QueryString["action"] == "viewProfile")
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                 
                        Guid viewUser = Guid.Parse(Request.QueryString["u"]);
                        Response.Redirect("member/Profile.aspx?user=" + viewUser);
                }
                else
                    Response.Redirect("RegisterNewUser.aspx");
            }
        }

        protected string FormatUsername(string username)
        {

            if (username.Length > 10)
            {
                username = username.Substring(0, 10);
                username += "...";
               
            }

            return username;
        }

        /// <summary>
        /// Gets random users for display on startpage
        /// </summary>
        private void GetRandomUsers()
        {
            List<ProfileComposite> randomUsers = wcf.GetRandomUsers(4);
            repeaterRandomUser.DataSource = randomUsers;
            repeaterRandomUser.DataBind();
        }

        /// <summary>
        /// Checks if current user is logged in, and uses a
        /// multiview to show different content on startpage based on loggedinstatus
        /// </summary>
        private void SetStartPage()
        {           
            bool IsLoggedIn = System.Web.HttpContext.Current.User.Identity.IsAuthenticated;

            if (IsLoggedIn)
                multiviewStartPageContent.SetActiveView(viewIsLoggedIn);
            else
                multiviewStartPageContent.SetActiveView(viewLogin);
        }

        /// <summary>
        /// Logs out a user
        /// </summary>
        public void LogOut()
        {
            FormsAuthentication.SignOut();
            Response.Redirect("/Default.aspx");
        }
        /// <summary>
        /// Executes when one of the random users on startpage is clicked. Checks first if 
        /// the user is logged in, then tries to redirect user to the profile. If not logged in, 
        /// the user gets redirected to the register page. 
        /// </summary>
        public void viewProfile()
        {

            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                try
                {
                    Guid viewUser = Guid.Parse(Request.QueryString["u"]);
                    Response.Redirect("/member/Profile.aspx?user=" + viewUser);
                }
                catch
                {
                    Response.Redirect("Error404.aspx");
                }
            }
            else
                Response.Redirect("RegisterNewUser.aspx");
        }
    }
}